package com.example.springboot_demo.tools;

import okhttp3.*;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.regex.Pattern;

/**
 * @author xiaojy
 * @version V1.1
 * @description 过滤html格式数据的工具类
 * @className FilterHtmlUtils.java
 * @date 14:09 2020/7/15
 * @since V1.1
 */
public class FilterHtmlUtils {

    public static String html2Text(String inputString) {
        String htmlStr = inputString; //含html标签的字符串
        String textStr = "";
        Pattern p_script;
        java.util.regex.Matcher m_script;
        Pattern p_style;
        java.util.regex.Matcher m_style;
        Pattern p_html;
        java.util.regex.Matcher m_html;

        String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; //定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script> }
        String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; //定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style> }
        String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式

        p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
        m_script = p_script.matcher(htmlStr);
        htmlStr = m_script.replaceAll(""); //过滤script标签

        p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
        m_style = p_style.matcher(htmlStr);
        htmlStr = m_style.replaceAll(""); //过滤style标签

        p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
        m_html = p_html.matcher(htmlStr);
        htmlStr = m_html.replaceAll(""); //过滤html标签

        htmlStr = htmlStr.replaceAll("\n", "").replaceAll(" ", "").replaceAll("&nbsp;", "").replaceAll("↵", "");//过滤换行符、过滤空格

        textStr = htmlStr;

        return textStr;//返回文本字符串
    }

    /**
     * 将html文件转换成带html标签的文本
     *
     * @param path
     * @since V1.0
     */
    public static String html2Str(String path) {
        StringBuilder str = new StringBuilder();
        InputStream inputStream = null;
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //设置超时间为3秒
            conn.setConnectTimeout(3 * 1000);
            //得到输入流
            inputStream = conn.getInputStream();

            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
            while (true) {
                String line = reader.readLine();
                if (line == null)
                    break;
                str.append(line);
            }
            if (reader != null) {
                reader.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str.toString();
    }

    public static void main(String[] args) {
        // 核心线程数，最大线程数，线程空闲时间，时间单位，工作队列
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                8,
                10,
                10L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>() // 使用无限大小的工作队列
        );
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter("/Users/jiangan/Desktop/error.txt");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        PrintWriter printWriter = new PrintWriter(fileWriter);
        try (BufferedReader br = new BufferedReader(new FileReader("/Users/jiangan/Desktop/tesgt.txt"))) {
            String line;
            while ((line = br.readLine()) != null) {
                // 在这里处理每一行的内容，例如打印到控制台
                String finalLine = line;
                executor.submit(() -> {
                    try {
                        URL url = new URL("https://smart-static.wosaimg.com/gallery/kaimai/" + finalLine);
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        // 设置请求方法为GET
                        connection.setRequestMethod("GET");
                        connection.setConnectTimeout(1000);
                        connection.setReadTimeout(1000);
                        connection.getResponseCode();
                    } catch (Exception e) {
                        System.out.println("异常:" + finalLine);
                        // 写入文本内容
                        printWriter.println(finalLine);
                        printWriter.flush();
                    }
                });
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        while (executor.getActiveCount() > 0) {
            try {
                Thread.sleep(10000);
                System.out.println("线程仍在执行:" + executor.getActiveCount() + "总量:" + executor.getTaskCount() + "队列:" + executor.getQueue().size());
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        executor.shutdownNow();
        printWriter.close();
    }

}
